package com.hbsd.paopaobackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbsd.paopaobackend.common.BaseResponse;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.common.ResultUtils;
import com.hbsd.paopaobackend.common.request.DeleteRequest;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Apply.AddApplyRequest;
import com.hbsd.paopaobackend.model.request.Apply.PassApplyByIdRequest;
import com.hbsd.paopaobackend.model.request.PageInfo;
import com.hbsd.paopaobackend.model.vo.Apply.ApplyListVO;
import com.hbsd.paopaobackend.service.ApplyService;
import com.hbsd.paopaobackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "好友申请模块")
@RestController
@RequestMapping("/apply")
// 配置跨域 allowCredentials: 允许携带cookie
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")

public class ApplyController {

    @Resource
    private ApplyService applyService;

    @Resource
    private UserService userService;

    @ApiOperation(value = "添加好友申请", notes = "添加好友申请 addApply")
    @PostMapping("/add")
    public BaseResponse<Boolean> addApply(@RequestBody AddApplyRequest applyRequest, HttpServletRequest request) {
        // 处理好友申请
        if(applyRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }


        Boolean result = applyService.addApply(applyRequest, loginUser);
        return ResultUtils.success(result);

    }


    /**
     * 获取好友申请列表
     * @param pageInfo
     * @param request
     * @return
     */
    @ApiOperation(value = "获取好友申请列表", notes = "获取好友申请列表 listApply")
    @GetMapping("/list/page")
    public BaseResponse<Page<ApplyListVO>> listApply(PageInfo pageInfo, HttpServletRequest request) {
        if(pageInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Page<ApplyListVO>  applyListPage = applyService.recommendUsers(pageInfo, loginUser);
        return ResultUtils.success(applyListPage);
    }

    /**
     * 通过好友申请
     * @param passApplyByIdRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "通过好友申请", notes = "通过好友申请 passApply")
    @PostMapping("/pass")
    public BaseResponse<Boolean> passApply(@RequestBody PassApplyByIdRequest passApplyByIdRequest, HttpServletRequest request) {
        // 处理好友申请
        long id = passApplyByIdRequest.getId();
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Boolean result = applyService.passApply(id, loginUser);

        return ResultUtils.success(result);
    }

    /**
     * 删除好友申请
     * @param deleteRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "删除好友申请", notes = "删除好友申请 deleteApply")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteApply(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        // 处理好友申请
        long id = deleteRequest.getId();
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Boolean result = applyService.deleteApply(id, loginUser);

        return ResultUtils.success(result);
    }
}
